package org.iryshka.skyserver;

import org.iryshka.logit.ConsoleFileLogger;
import org.iryshka.logit.ConsoleFileLogger.WritingTo;

import java.io.*;
import java.util.ArrayList;

public class ConfigAnalyzer {
	
	private static File 	fileConfig;// CONFIGURATION DATA file
	private static String 	configLine;
	
	private static String 	serverDirectory;

	private static Integer 	numRequests;
	private static Integer 	port;
	private static boolean 	enableListing;
	private static File 	fileOfForbiddenRequests;
	private static ArrayList<String> listOfForbiddenIP;
	
	private static final String DEFAULT_CONFIG_FILE = "C:/temp/skywebserver.config.properties";
	
	public static File getFileConfig() {
		return fileConfig;
	}

	public static String getServerDirectory() {
		return serverDirectory;
	}

	public static Integer getNumRequests() {
		return numRequests;
	}

	public static Integer getPort() {
		return port;
	}

	public static boolean isListingEnabled() {
		return enableListing;
	}

	public static File getFileOfForbiddenRequests() {
		return fileOfForbiddenRequests;
	}

	public static ArrayList<String> getListOfForbiddenIP() {
		return listOfForbiddenIP;
	}
	
	private class WrongConfigDataException extends RuntimeException{

		public WrongConfigDataException(String exceptionMessage) {
			super (exceptionMessage);
		}
	}

	public void parseConfigFile (String[] args) throws IOException {
		
		if (args.length > 0){
			fileConfig = new File(args[0]);
			System.out.println("Configuration started load from mentioned file");
		}
		else{
			fileConfig = new File(DEFAULT_CONFIG_FILE);
			System.out.println("Configuration started load from default file");
		}

		try (BufferedReader readingLineStream = new BufferedReader (new FileReader (fileConfig))){
			while ((configLine = readingLineStream.readLine()) != null) {
				readConfigValue(configLine);
			}
		}
		
		System.out.println("Configuration has been successfully loaded");
	}
	
	private void readConfigValue (String strToCheck) throws IOException{
		
		String[] subStrToCheck = strToCheck.split("=");
		
		if (subStrToCheck.length < 2 ){
			// optional parameters 'listOfForbiddenIP' and 'LogPath' may contain empty values
			if (!subStrToCheck[0].contains("listOfForbiddenIP") && !subStrToCheck[0].contains("LogPath")) {
				throw new WrongConfigDataException("Please enter data for " + subStrToCheck[0] + " to let SkyWebServer work ...\n");
			}			
		}
		
		if(subStrToCheck[0].contains("ServerDirectory")){
			serverDirectory = subStrToCheck[1];
		}
		else if(subStrToCheck[0].contains("NumRequests")){
			numRequests = new Integer (subStrToCheck[1]);
			if (numRequests < 2 || numRequests > 10){
				throw new WrongConfigDataException("Configuration data for NumRequests is incorrect. Only numbers from 2 to 10 is allowed...\n");
			}
		}
		else if(subStrToCheck[0].contains("Port")){
			port = new Integer (subStrToCheck[1]); 
			if (port < 1024 || port > 65536){
				throw new WrongConfigDataException("Configuration data for Port is incorrect. Only numbers from 1025 to 65536 is allowed...\n");
			}
		}
		else if(subStrToCheck[0].contains("Listing")){
			enableListing = (subStrToCheck[1]).equals("ON") ?  true : false;
		}
		else if(subStrToCheck[0].contains("LogTo")){
			ConsoleFileLogger.whereToWrite(ConsoleFileLogger.WritingTo.valueOf(subStrToCheck[1]));
		}
		else if(subStrToCheck[0].contains("LogPath")){
			if(ConsoleFileLogger.destToWrite == WritingTo.FILE ||
				ConsoleFileLogger.destToWrite == WritingTo.FILE_CONSOLE){
				if (subStrToCheck.length > 2 ){
					ConsoleFileLogger.setFileToWrite(subStrToCheck[1]);
				}
			}
		}
		else if(subStrToCheck[0].contains("listOfForbiddenIP")){
			if(subStrToCheck.length > 2 ){
				fileOfForbiddenRequests = new File(subStrToCheck[1]);
				listOfForbiddenIP = new ArrayList<String>();
				String IPForbiddenNum = null;
				try (BufferedReader readingLineStream = new BufferedReader (new FileReader (fileOfForbiddenRequests))){
					while ((IPForbiddenNum = readingLineStream.readLine()) != null){
						listOfForbiddenIP.add("/" + IPForbiddenNum);
					}
				}
			}
		}
	}
}
